<!--  -->
<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect :show="scene != 0" @getCategoryId="getCategoryId" />
    </el-card>
    <el-card>
      <!-- 展示Spu列表结构 -->
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加Spu</el-button>
        <el-table style="width: 100%" border :data="spuList">
          <el-table-column type="index" label="序号" width="100px" />
          <el-table-column prop="spuName" label="SPU名称" width="width" />
          <el-table-column prop="description" label="SPU描述" width="width" />
          <el-table-column prop="prop" label="操作" width="width">
            <!-- eslint-disable-next-line -->
            <template slot-scope="{row, $index}">
              <!-- <el-button type="success" icon="el-icon-plus" size="mini" />
              <el-button type="warning" icon="el-icon-edit" size="mini" />
              <el-button type="info" icon="el-icon-info" size="mini" />
              <el-button type="danger" icon="el-icon-delete" size="mini" /> -->
              <HintButton type="success" icon="el-icon-plus" size="mini" title="添加Sku" @click="addSku(row)" />
              <HintButton type="warning" icon="el-icon-edit" size="mini" title="修改Spu" @click="updateSpu(row)" />
              <HintButton type="info" icon="el-icon-info" size="mini" title="查看当前Spu全部Sku列表" @click="handlerVisible(row)" />
              <el-popconfirm title="确定要删除这个Spu吗？" @onConfirm="deleteSpu(row)">
                <HintButton slot="reference" type="danger" icon="el-icon-delete" size="mini" title="删除Spu" />
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper, ->, sizes, total"
          :total="total"
          hide-on-single-page
          @size-change="handleSizeChange"
          @current-change="getSpuList"
        />
      </div>
      <!-- 添加、修改SPU -->
      <!-- <div v-show="scene == 1"></div> -->
      <SpuForm v-show="scene == 1" ref="spu" @changeScene="changeScene" />
      <!-- 添加SKU -->
      <!-- <div v-show="scene == 2"></div> -->
      <SkuForm v-show="scene == 2" ref="sku" @changeSceneZero="changeSceneZero" />
    </el-card>
    <el-dialog :title="`${spu.spuName}的列表`" :visible.sync="dialogTableVisible" :before-close="closeSkuList">
      <el-table v-loading="loading" :data="skuList" style="width: 100%" border>
        <el-table-column prop="skuName" label="名称" width="width" />
        <el-table-column prop="price" label="价格" width="200" />
        <el-table-column prop="weight" label="重量" width="200" />
        <el-table-column label="默认图片" width="150">
          <!-- eslint-disable-next-line -->
          <template slot-scope="{row, $index}">
            <img :src="row.skuDefaultImg" style="width:100px;height:100px" alt="">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './SpuForm'
import SkuForm from './SkuForm'
export default {
  name: 'Spu',
  components: {
    SpuForm,
    SkuForm
  },
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      spuList: [],
      page: 1,
      limit: 3,
      total: 0,
      // 对下方card状态进行控制 0-spu列表 1-添加\修改spu 2-添加sku
      scene: 0,
      dialogTableVisible: false,
      spu: {},
      skuList: [],
      loading: true
    }
  },
  mounted() {
    this.getSpuList()
  },
  methods: {
    // 三级列表自定义事件，依次将categoryId传递给父组件
    getCategoryId({ categoryId, level }) {
      if (level === 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level === 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else {
        this.category3Id = categoryId
        this.getSpuList()
      }
    },
    // 获取Spu列表
    async getSpuList(pages = 1) {
      this.page = pages
      const { page, limit, category3Id } = this
      const result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      if (result.code === 200) {
        this.total = result.data.total
        this.spuList = result.data.records
      } else {
        this.$message({
          type: 'info',
          message: '服务器异常!'
        })
      }
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    // 添加SPU
    addSpu() {
      this.scene = 1
      // 通知子组件发请求获取品牌列表信息和全部销售属性信息
      this.$refs.spu.addSpuData(this.category3Id)
    },
    // 修改SPU
    updateSpu(row) {
      this.scene = 1
      // 点击修改时 获取子组件SpuForm,并调用子组件方法
      this.$refs.spu.initSpuData(row)
    },
    // Spu场景点击取消,回到Spu列表场景
    changeScene(scene, flag) {
      this.scene = scene
      if (flag === '修改') {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    // 删除spu
    async deleteSpu(spu) {
      const result = await this.$API.spu.reqDeleteSpu(spu.id)
      if (result.code === 200) {
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        // spu个数大于1，删除时停留当前页，否则回到上一页
        this.$getSpuList(this.records.length > 1 ? this.page : this.page - 1)
      } else {
        this.$message({
          type: 'info',
          message: '删除失败，请稍后再试!'
        })
      }
    },
    // 添加sku
    addSku(spu) {
      this.scene = 2
      // 子组件发请求
      this.$refs.sku.initSkuData(this.category1Id, this.category2Id, spu)
    },
    // 新增sku，点击取消 回到页面
    changeSceneZero(scene) {
      this.scene = scene
    },
    // 查看sku列表
    async handlerVisible(spu) {
      this.spu = spu
      this.dialogTableVisible = true

      const result = await this.$API.spu.reqSkuList(spu.id)
      if (result.code === 200) {
        this.skuList = result.data
        this.loading = false
      }
    },
    closeSkuList(done) {
      this.loading = true
      this.skuList = []
      done()
    }
  }
}
</script>

<style  scoped>

</style>
